package com.lanou.web.controller;

import com.lanou.entity.SearchResult;
import com.lanou.service.SearchItemService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;


@Controller
public class SearchItemController {

    @Autowired
    private SearchItemService searchItemService;

    @ApiOperation("关键字查询商品")
    @GetMapping("/search")
    public String searchItemList(@RequestParam("q")String query,
                                 @RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "60") Integer rows,
                                 Model model){
        SearchResult searchResult = searchItemService.searchItemList(query,page,rows);
        System.out.println(searchResult);
        model.addAttribute("itemList",searchResult.getDocuments());
        model.addAttribute("totalPages",searchResult.getPagenums());
        model.addAttribute("query",query);
        model.addAttribute("page",page);
        return "search";
    }

}
